package com.wawj.compplugin.utils


class CmdUtil {
    private static boolean isDebug = false

    static setDebug(boolean isDebug) {
        this.isDebug = isDebug
    }

    static Boolean execute(String command) {

        return execute(command, null)
    }

    static Boolean execute(String command, File dir) {
        return execute(command, dir, true)
    }

    static Boolean execute(String command, File dir, Boolean interruptWhenCmdFailed) {
        def process = Runtime.getRuntime().exec(command, null, dir)
        def result = process.waitFor()
        if (result != 0) {
            def failureMsg = "执行命令失败\n命令：${command}\n目录：${dir}\n错误原因：${process.err.text}"
            if (interruptWhenCmdFailed) {
                throw new CmdExecuteException(failureMsg)

            } else {
                println(failureMsg)
            }
        } else {
            if (isDebug)
                println("执行命令成功\n命令：${command}\n输出结果：${process.inputStream.text}")
        }
        return result == 0
    }


    static Boolean execute(String[] command, File dir, Boolean interruptWhenCmdFailed) {
        def process = Runtime.getRuntime().exec(command, null, dir)
        def result = process.waitFor()
        if (result != 0) {
            def failureMsg = "执行命令失败\n命令：${command}\n目录：${dir}\n错误原因：${process.err.text}"
            if (interruptWhenCmdFailed) {
                throw new CmdExecuteException(failureMsg)

            } else {
                println(failureMsg)
            }
        } else {
            if (isDebug)
                println("执行命令成功\n命令：${command}\n输出结果：${process.inputStream.text}")
        }
        return result == 0
    }

    static String executeForOutput(String command) {
        return executeForOutput(command, null, true)
    }

    static String executeForOutput(String command, File dir) {
        return executeForOutput(command, dir, true)
    }


    static String executeForOutput(String[] command, File dir, Boolean interruptWhenCmdFailed) {
        def process = Runtime.getRuntime().exec(command, null, dir)
        def result = process.waitFor()
        if (result != 0) {
            def failureMsg = "执行命令失败\n命令：${command}\n目录：${dir}\n错误原因：${process.err.text}"
            if (interruptWhenCmdFailed) {
                throw new RuntimeException(failureMsg)
            } else {
                println(failureMsg)
                return failureMsg
            }
        } else {
            def resultText = process.inputStream.text
            if (isDebug)
                println("执行命令成功\n命令：${command}\n输出结果：${resultText}")
            return resultText
        }
    }

    static String executeForOutput(String command, File dir, Boolean interruptWhenCmdFailed) {
        def process = Runtime.getRuntime().exec(command, null, dir)
        def result = process.waitFor()
        if (result != 0) {
            def failureMsg = "执行命令失败\n命令：${command}\n目录：${dir}\n错误原因：${process.err.text}"
            if (interruptWhenCmdFailed) {
                throw new RuntimeException(failureMsg)
            } else {
                println(failureMsg)
                return failureMsg
            }
        } else {
            def resultText = process.inputStream.text
            if (isDebug)
                println("执行命令成功\n命令：${command}\n输出结果：${resultText}")
            return resultText
        }
    }

}
